#include "stdafx.h"
#include "InputKeyboard.h"


InputKeyboard::InputKeyboard(void)
{
	registerKeys();
	currentState = 0;
#ifdef _WIN32
	GetKeyboardState(keyState[currentState]);
	POINT pt;
	GetCursorPos(&pt);
	mousePositionCurrent.x = (float)pt.x;
	mousePositionCurrent.y = (float)pt.y;
#endif
	mousePositionLast = mousePositionCurrent;
}


InputKeyboard::~InputKeyboard(void)
{
}

bool InputKeyboard::isKeyDown(int key) const
{
	assert(key>=0 && key<256);

	return isKeyDownInState(currentState,key);
}

bool InputKeyboard::didKeyJustGoDown(int key) const
{
	return isKeyDownInState(currentState,key) && !isKeyDownInState(currentState^1,key);
}

bool InputKeyboard::didKeyJustGoUp(int key) const
{
	return !isKeyDownInState(currentState,key) && isKeyDownInState(currentState^1,key);
}

inline bool InputKeyboard::isKeyDownInState(int state, int key) const
{
	assert(state>=0 && state<2);
	return (keyState[state][key] & 0x80) != 0;
}

void InputKeyboard::update(float dt)
{
	currentState ^= 1;
	mousePositionLast = mousePositionCurrent;
#ifdef _WIN32
	GetKeyboardState(keyState[currentState]);
	POINT pt;
	GetCursorPos(&pt);
	mousePositionCurrent.x = (float)pt.x;
	mousePositionCurrent.y = (float)pt.y;
#endif
}

void InputKeyboard::getMove(MoveParam& mp) const
{
	mp.left = mousePositionLast - mousePositionCurrent;
}

void InputKeyboard::registerKeys()
{
	mapKeys["VK_LBUTTON"] = VK_LBUTTON;
	mapKeys["VK_RBUTTON"] = VK_RBUTTON;
	mapKeys["VK_CANCEL"] = VK_CANCEL;
	mapKeys["VK_MBUTTON"] = VK_MBUTTON;
	mapKeys["VK_BACK"] = VK_BACK;
	mapKeys["VK_TAB"] = VK_TAB;
	mapKeys["VK_CLEAR"] = VK_CLEAR;
	mapKeys["VK_RETURN"] = VK_RETURN;
	mapKeys["VK_SHIFT"] = VK_SHIFT;
	mapKeys["VK_CONTROL"] = VK_CONTROL;
	mapKeys["VK_MENU"] = VK_MENU;
	mapKeys["VK_PAUSE"] = VK_PAUSE;
	mapKeys["VK_CAPITAL"] = VK_CAPITAL;
	mapKeys["VK_KANA"] = VK_KANA;
	mapKeys["VK_HANGEUL"] = VK_HANGEUL;
	mapKeys["VK_HANGUL"] = VK_HANGUL;
	mapKeys["VK_JUNJA"] = VK_JUNJA;
	mapKeys["VK_FINAL"] = VK_FINAL;
	mapKeys["VK_HANJA"] = VK_HANJA;
	mapKeys["VK_KANJI"] = VK_KANJI;
	mapKeys["VK_ESCAPE"] = VK_ESCAPE;
	mapKeys["VK_CONVERT"] = VK_CONVERT;
	mapKeys["VK_NONCONVERT"] = VK_NONCONVERT;
	mapKeys["VK_ACCEPT"] = VK_ACCEPT;
	mapKeys["VK_MODECHANGE"] = VK_MODECHANGE;
	mapKeys["VK_SPACE"] = VK_SPACE;
	mapKeys["VK_PRIOR"] = VK_PRIOR;
	mapKeys["VK_NEXT"] = VK_NEXT;
	mapKeys["VK_END"] = VK_END;
	mapKeys["VK_HOME"] = VK_HOME;
	mapKeys["VK_LEFT"] = VK_LEFT;
	mapKeys["VK_UP"] = VK_UP;
	mapKeys["VK_RIGHT"] = VK_RIGHT;
	mapKeys["VK_DOWN"] = VK_DOWN;
	mapKeys["VK_SELECT"] = VK_SELECT;
	mapKeys["VK_PRINT"] = VK_PRINT;
	mapKeys["VK_EXECUTE"] = VK_EXECUTE;
	mapKeys["VK_SNAPSHOT"] = VK_SNAPSHOT;
	mapKeys["VK_INSERT"] = VK_INSERT;
	mapKeys["VK_DELETE"] = VK_DELETE;
	mapKeys["VK_HELP"] = VK_HELP;
	mapKeys["VK_LWIN"] = VK_LWIN;
	mapKeys["VK_RWIN"] = VK_RWIN;
	mapKeys["VK_APPS"] = VK_APPS;
	mapKeys["VK_SLEEP"] = VK_SLEEP;
	mapKeys["VK_NUMPAD0"] = VK_NUMPAD0;
	mapKeys["VK_NUMPAD1"] = VK_NUMPAD1;
	mapKeys["VK_NUMPAD2"] = VK_NUMPAD2;
	mapKeys["VK_NUMPAD3"] = VK_NUMPAD3;
	mapKeys["VK_NUMPAD4"] = VK_NUMPAD4;
	mapKeys["VK_NUMPAD5"] = VK_NUMPAD5;
	mapKeys["VK_NUMPAD6"] = VK_NUMPAD6;
	mapKeys["VK_NUMPAD7"] = VK_NUMPAD7;
	mapKeys["VK_NUMPAD8"] = VK_NUMPAD8;
	mapKeys["VK_NUMPAD9"] = VK_NUMPAD9;
	mapKeys["VK_MULTIPLY"] = VK_MULTIPLY;
	mapKeys["VK_ADD"] = VK_ADD;
	mapKeys["VK_SEPARATOR"] = VK_SEPARATOR;
	mapKeys["VK_SUBTRACT"] = VK_SUBTRACT;
	mapKeys["VK_DECIMAL"] = VK_DECIMAL;
	mapKeys["VK_DIVIDE"] = VK_DIVIDE;
	mapKeys["VK_F1"] = VK_F1;
	mapKeys["VK_F2"] = VK_F2;
	mapKeys["VK_F3"] = VK_F3;
	mapKeys["VK_F4"] = VK_F4;
	mapKeys["VK_F5"] = VK_F5;
	mapKeys["VK_F6"] = VK_F6;
	mapKeys["VK_F7"] = VK_F7;
	mapKeys["VK_F8"] = VK_F8;
	mapKeys["VK_F9"] = VK_F9;
	mapKeys["VK_F10"] = VK_F10;
	mapKeys["VK_F11"] = VK_F11;
	mapKeys["VK_F12"] = VK_F12;
	mapKeys["VK_F13"] = VK_F13;
	mapKeys["VK_F14"] = VK_F14;
	mapKeys["VK_F15"] = VK_F15;
	mapKeys["VK_F16"] = VK_F16;
	mapKeys["VK_F17"] = VK_F17;
	mapKeys["VK_F18"] = VK_F18;
	mapKeys["VK_F19"] = VK_F19;
	mapKeys["VK_F20"] = VK_F20;
	mapKeys["VK_F21"] = VK_F21;
	mapKeys["VK_F22"] = VK_F22;
	mapKeys["VK_F23"] = VK_F23;
	mapKeys["VK_F24"] = VK_F24;
	mapKeys["VK_NUMLOCK"] = VK_NUMLOCK;
	mapKeys["VK_SCROLL"] = VK_SCROLL;
	mapKeys["A"] = 'A';
	mapKeys["B"] = 'B';
	mapKeys["C"] = 'C';
	mapKeys["D"] = 'D';
	mapKeys["E"] = 'E';
	mapKeys["F"] = 'F';
	mapKeys["G"] = 'G';
	mapKeys["H"] = 'H';
	mapKeys["I"] = 'I';
	mapKeys["J"] = 'J';
	mapKeys["K"] = 'K';
	mapKeys["L"] = 'L';
	mapKeys["M"] = 'M';
	mapKeys["N"] = 'N';
	mapKeys["O"] = 'O';
	mapKeys["P"] = 'P';
	mapKeys["Q"] = 'Q';
	mapKeys["R"] = 'R';
	mapKeys["S"] = 'S';
	mapKeys["T"] = 'T';
	mapKeys["U"] = 'U';
	mapKeys["V"] = 'V';
	mapKeys["W"] = 'W';
	mapKeys["X"] = 'X';
	mapKeys["Y"] = 'Y';
	mapKeys["Z"] = 'Z';
	mapKeys["VK_LSHIFT"] = VK_LSHIFT;
	mapKeys["VK_RSHIFT"] = VK_RSHIFT;
}

InputDevice* createKeyboardAndMouseInput()
{
	return new InputKeyboard;
}